package cc.SSM.utils;






import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

/**
 * Created by rongmc on 16/6/2.
 */
public class EhcacheUtil {

    /**
     * 获取缓存对象
     * @param cacheManager
     * @return
     */
    private  static Cache getCache(CacheManager cacheManager, String cacheName) {
        if( !(ParamUtil.isNullOrEmptyOrZero(cacheManager) && ParamUtil.isNullOrEmptyOrZero(cacheName)) ){
            Cache cache=cacheManager.getCache(cacheName);
            return  cache ;
        }
        return null ;
    }

    /**
     * 写入缓存
     * @param key
     * @param value
     */
    public  static  void set(CacheManager cacheManager,String cacheName,String key, String value) {
        if(!(ParamUtil.isNullOrEmptyOrZero(cacheManager) && ParamUtil.isNullOrEmptyOrZero(cacheName)
        &&ParamUtil.isNullOrEmptyOrZero(key)) ){
            Cache cache=getCache(cacheManager,cacheName);
            cache.put(key,value);
        }
    }

    /**
     * 读取缓存值
     * @param key
     * @return
     */
    public static String get(CacheManager cacheManager,String cacheName,String key) {
        Cache cache=getCache(cacheManager,cacheName);
        return  cache.get(key,String.class);
    }

}
